<?php
namespace app\yunda\admin;

use app\common\builder\ZBuilder;
use app\yunda\model\Danhao as DanhaoModel;
use think\Db;

class Danhao extends Base{
    public function index()
    {
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $map['delete_time']=NULL;
        $data = Db::table('dp_yd_danhao',true)
            // ->view('xiaoan_post',['title'],'xiaoan_post.id=xiaoan_comment.post_id')
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){
                $arr=['否','是'];
                $item['is_use']=$arr[$item['is_use']];
                return $item;
            });
        return ZBuilder::make('table')
            ->setTableName('yd_danhao')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['order_sn','快递单号'],
                ['is_use','是否已使用'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', [],[])
            ->addRightButton('edit',[],[])
            ->addTopButtons(['delete'])
            ->setRowList($data)
            //->addFilter(['title' => 'admin_action', 'username'])
            ->setSearch(['id' => 'ID', 'order_sn' => '快递单号'])
            ->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            // 第二部 验证数据
            $result = $this->validate($data,'Danhao');
            if($result!==true) $this->error($result);
            // 第三部 存储
            DanhaoModel::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['text','order_sn','快递单号','必须'],
            ])
            ->fetch();
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $result = $this->validate($data,'Danhao');
            if($result!==true) $this->error($result);
            DanhaoModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        $data = DanhaoModel::get($id);
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],  // 多增加一个影藏ID 即可
                ['text','order_sn','快递单号','必须'],
            ])
            ->setFormData($data)
            ->fetch();
    }
    //软删除
    public function delete($record = [])
    {
        $data=request()->param();
        DanhaoModel::destroy(['id'=>['in',$data['ids']]]);//删除多条
        $this->success('删除成功', null, '_parent_reload');
    }
}